<script lang="ts" setup>

import {FormInst, NButton, NSpace} from "naive-ui";
import {reactive, ref} from "vue";
import {util} from "../../../../ts/util";
import post from "../../../../axios";

interface Vo {
  configCode: string,
  tableCode: string,
  overwrite: string
}

const emit = defineEmits(["close", "refresh"])
let init = defineProps<{
  tableCode: string | null,
  options: any
}>()
let formRef = ref<FormInst | null>(null)
let form = reactive<{
  hideColumn: Array<string>,
  model: Vo,
  rules: any,
  cancel: Function,
  ok: Function
}>({
  hideColumn: ['id'],
  model: {
    configCode: '',
    tableCode: '',
    overwrite: 'false'
  },
  rules: {
    configCode: [
      {required: true, message: '请选择配置'}
    ]
  },
  cancel: () => {
    emit("close")
  },
  ok: () => {
    formRef.value?.validate((errors: any) => {
      if (errors) {
        util.msg?.error('请检查填写内容')
      } else {
        post("/cloud-code/tableInfo/build", Object.assign(form.model, init)).then(() => {
          util.msg?.success('生成成功')
          emit("refresh")
          emit("close")
        })
      }
    })
  }
})

let options = reactive({
  configCode: []
})

post("/cloud-code/configInfo/auto/selectConfigCodeOption", {}).then((res: any) => {
  options.configCode = res
})
</script>

<template>
  <n-modal
      :bordered="false"
      :mask-closable="false"
      preset="card"
      style="width: 60%;"
      title="代码生成"
      @close="form.cancel"
  >
    <n-form
        ref="formRef"
        :model="form.model"
        :rules="form.rules"
        label-align="right"
        label-placement="left"
        label-width="auto"
    >
      <n-grid :cols="24" :x-gap="24">
        <n-form-item-gi hidden label="table编号" path="tableCode" span="24">
          <n-input v-model:value="form.model.tableCode"/>
        </n-form-item-gi>

        <n-form-item-gi label="选择配置" path="configCode" span="24">
          <n-select v-model:value="form.model.configCode" :options="options.configCode"/>
        </n-form-item-gi>

        <n-form-item-gi label="是否覆盖" path="overwrite" span="24">
          <n-radio-group v-model:value="form.model.overwrite" default-value="false"
                         @update-value="(v: string) => form.model.overwrite = v">
            <n-space>
              <n-radio checked value="false">
                否
              </n-radio>
              <n-radio value="true">
                是
              </n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item-gi>

      </n-grid>
    </n-form>
    <template #action>
      <n-space justify="end">
        <n-button
            type="info"
            @click="form.cancel"
        >
          取消
        </n-button>
        <n-button
            v-limitClick="5000"
            type="primary"
            @click="form.ok">
          生成
        </n-button>
      </n-space>
    </template>
  </n-modal>
</template>

<style scoped>

</style>